package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.DailyFeedBackStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.DailyOrderStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/6/16.
 */

public class EngineeringInstallStatusFormanagerModel implements  IEngineeringInstallStatusFormanagerModel{

    @Override
    public void engineeringInstallStatusFormanager(final OnEngineeringInstallStatusFormanagerListener engineeringInstallStatusFormanagerListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    String url = ProjectUtil.INSTALL_STATU_FOR_MANAGER;
                    HttpClient client = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(url);

                    HttpResponse response;

                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);


                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != engineeringInstallStatusFormanagerListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray dataJsonArr = new JSONArray(json.getString("data"));
                            if(null != dataJsonArr){
                                List<EngineeringInstallStatuBean> engineeringInstallStatuBeanList = new ArrayList<EngineeringInstallStatuBean>();

                                for(int j = 0; j < dataJsonArr.length(); j++){
                                    JSONObject dataJsonObj = (JSONObject) dataJsonArr.get(j);

                                    EngineeringInstallStatuBean engineeringInstallStatuBean = new EngineeringInstallStatuBean();

                                    String dailyFeedbackByDateListArrStr = dataJsonObj.getString("dailyFeedbackByDateList");
                                    if (null != dailyFeedbackByDateListArrStr && !dailyFeedbackByDateListArrStr.equals("null") && !TextUtils.isEmpty(dailyFeedbackByDateListArrStr)) {
                                        JSONArray dailyFeedbackByDateListArr = new JSONArray(dailyFeedbackByDateListArrStr);
                                        List<DailyFeedBackStatuBean> dailyFeedBackStatuBeanList = new ArrayList<DailyFeedBackStatuBean>();

                                        for (int i = 0; i < dailyFeedbackByDateListArr.length(); i++) {
                                            JSONObject jsonObject = (JSONObject) dailyFeedbackByDateListArr.get(i);
                                            DailyFeedBackStatuBean dailyFeedBackStatuBean = new DailyFeedBackStatuBean();

                                            dailyFeedBackStatuBean.setDailyFeedbackId(jsonObject.getString("dailyFeedbackId"));
                                            dailyFeedBackStatuBean.setDateStr(jsonObject.getString("dateStr"));
                                            String state = jsonObject.getString("state");
                                            if (null != state) {
                                                if (state.equals("EXCEPTION")) {
                                                    dailyFeedBackStatuBean.setState(CommonEnum.FeedBackState.EXCEPTION);
                                                } else if (state.equals("WAIT_CONFIRM")) {
                                                    dailyFeedBackStatuBean.setState(CommonEnum.FeedBackState.WAIT_CONFIRM);
                                                } else if (state.equals("WORKING")) {
                                                    dailyFeedBackStatuBean.setState(CommonEnum.FeedBackState.WORKING);

                                                } else if (state.equals("COMPLETE")) {
                                                    dailyFeedBackStatuBean.setState(CommonEnum.FeedBackState.COMPLETE);
                                                } else if (state.equals("OUT_OF_PLAN")) {
                                                    dailyFeedBackStatuBean.setState(CommonEnum.FeedBackState.OUT_OF_PLAN);
                                                } else if (state.equals("NO_FEEDBACK")) {
                                                    dailyFeedBackStatuBean.setState(CommonEnum.FeedBackState.NO_FEEDBACK);
                                                } else if (state.equals("PAUSE")) {
                                                    dailyFeedBackStatuBean.setState(CommonEnum.FeedBackState.PAUSE);

                                                }else if (state.equals("CREATE")) {
                                                    dailyFeedBackStatuBean.setState(CommonEnum.FeedBackState.CREATE);

                                                }
                                            }

                                            dailyFeedBackStatuBeanList.add(dailyFeedBackStatuBean);

                                        }

                                        engineeringInstallStatuBean.setDailyFeedbackByDateList(dailyFeedBackStatuBeanList);

                                    }

                                    String dailyOrderStatusListArrStr = dataJsonObj.getString("dailyOrderStatusList");
                                    if (null != dailyOrderStatusListArrStr && !dailyOrderStatusListArrStr.equals("null") && !TextUtils.isEmpty(dailyOrderStatusListArrStr)) {
                                        JSONArray dailyOrderStatusListArr = new JSONArray(dailyOrderStatusListArrStr);
                                        List<DailyOrderStatuBean> dailyOrderStatuBeanList = new ArrayList<DailyOrderStatuBean>();

                                        for (int i = 0; i < dailyOrderStatusListArr.length(); i++) {
                                            JSONObject jsonObject = (JSONObject) dailyOrderStatusListArr.get(i);
                                            DailyOrderStatuBean dailyOrderStatuBean = new DailyOrderStatuBean();

                                            dailyOrderStatuBean.setDateStr(jsonObject.getString("dateStr"));

                                            String state = jsonObject.getString("state");
                                            if (null != state) {
                                                if (state.equals("PLAN_CYCLE")) {
                                                    dailyOrderStatuBean.setState(CommonEnum.OrderCycleStatus.PLAN_CYCLE);
                                                } else if (state.equals("CYCLE_ADJUSTMENT")) {
                                                    dailyOrderStatuBean.setState(CommonEnum.OrderCycleStatus.CYCLE_ADJUSTMENT);
                                                } else if (state.equals("READJUSTMENT")) {
                                                    dailyOrderStatuBean.setState(CommonEnum.OrderCycleStatus.READJUSTMENT);

                                                } else if (state.equals("PAUSE")) {
                                                    dailyOrderStatuBean.setState(CommonEnum.OrderCycleStatus.PAUSE);
                                                } else if (state.equals("COMPLETE")) {
                                                    dailyOrderStatuBean.setState(CommonEnum.OrderCycleStatus.COMPLETE);
                                                }
                                            }

                                            dailyOrderStatuBeanList.add(dailyOrderStatuBean);

                                        }

                                        engineeringInstallStatuBean.setDailyOrderStatusList(dailyOrderStatuBeanList);

                                    }

                                    engineeringInstallStatuBean.setEngineeringAddress(dataJsonObj.getString("engineeringAddress"));
                                    engineeringInstallStatuBean.setEngineeringName(dataJsonObj.getString("engineeringName"));


                                    String latStr = dataJsonObj.getString("lat");
                                    if (!TextUtils.isEmpty(latStr)) {
                                        engineeringInstallStatuBean.setLat(Double.parseDouble(latStr));
                                    }

                                    String lonStr = dataJsonObj.getString("lon");
                                    if (!TextUtils.isEmpty(lonStr)) {
                                        engineeringInstallStatuBean.setLon(Double.parseDouble(lonStr));
                                    }


                                    engineeringInstallStatuBean.setOrderId(dataJsonObj.getString("orderId"));
                                    engineeringInstallStatuBean.setProjectName(dataJsonObj.getString("projectName"));


                                    engineeringInstallStatuBeanList.add(engineeringInstallStatuBean);

                                }

                                engineeringInstallStatusFormanagerListener.engineeringInstallStatusFormanagerSuccessed(engineeringInstallStatuBeanList);
                            }


//                            engineeringInstallStatusFormanagerListener.engineeringInstallStatusFormanagerSuccessed(engineeringInstallStatuBean);




                        } else {
                            String desc = code;
                            if (!json.isNull("desc")) {
                                desc += "," + json.getString("desc");
                            }
                            engineeringInstallStatusFormanagerListener.engineeringInstallStatusFormanagerFailed(desc);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnEngineeringInstallStatusFormanagerListener {
        void engineeringInstallStatusFormanagerSuccessed(List<EngineeringInstallStatuBean> engineeringInstallStatuBeanList);

        void engineeringInstallStatusFormanagerFailed(String desc);
    }
}
